import { Component, ElementRef, OnInit, Output, Input, ViewChild, AfterViewInit, EventEmitter } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';

import { RouterModule, Router, ActivatedRoute } from '@angular/router';
import { Service } from '../../../server/server';
import { Pagination, PaginationUrl } from '../../../public/serve/pcrudInfo';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
declare var $: any;


@Component({
  selector: 'app-central-table',
  templateUrl: './central-table.component.html'
})
export class CentralTableComponent implements OnInit {

  @Input() tableNumber: number;

  @Input() searchData: any;

  right: any = {
    "central": {},
  };
  constructor(
    private http: HttpClient, 
    private server: Service, 
    private router: Router, 
    private tableListService: CudTableService, 
    private removeTableService: RemoveTableService
  ) {
    this.right = $.extend(this.right, server.getRight());
  }
  public userId: any;
  public rolesData: string[];
  public rolesDetailsData = [];

  ngOnInit() {
    //console.log(this.userId)
    this.userId = this.server.getuser();
    this.getTabList(1, 20, this.searchData);
  }

  @Output()
  tabeListOutput: EventEmitter<string> = new EventEmitter();

  public tabVal: string[];
  // 表格
  getTabList(pageNumber, pageSize, name) {

    let url = this.server.baseUrl + 'Service/Center/getCenterList';
    let data = { pageNumber: pageNumber, pageSize: pageSize, name: name }

    this.tableListService.postRequest(url, data).then((data: any) => {
      if (data.result_code == '0000') {
        this.tabVal = data.result;
        this.tabeListOutput.emit(data.length);
      }
      // console.log(data);
    }).catch();
  }



   // 当前页（默认为第一页）
   public pageNumber: number = 1;
   // 每页显示多少条数据（默认每页显示20条）
   public pageSize: number = 20;
   // id
   public id: string = '0';
 
   //#delete
 
   // 全选
   public isSelectAll: boolean = false;
   public isChose: boolean = false;
   isSelect: boolean = true;
   isDisable: any;              // 按钮（启用/禁用）
 
   @Output() isSelectFromTable = new EventEmitter<boolean>();
  
   selectAll(bool, tabVal) {
     this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'id');
     this.isSelect = !this.isSelectAll;
     this.isSelectFromTable.emit(this.isSelect);
   }
 
   // 单选（多选）
   chose(bool, id, length) {
     this.isDisable = this.removeTableService.removeChose(bool, id, length);
     this.isSelectAll = this.isDisable.bool;
     this.isDisable.len ? this.isSelect = false : this.isSelect = true;
     this.isSelectFromTable.emit(this.isSelect);
   }
 
   // 没有数据时取消勾选全选按钮
   cancelSelectAll() {
     this.isSelectAll = false;
     this.isChose = false;
   }

  public subList = { id: '', name: '', description: '', createdate: '',operateType:"1" };
  //input null是否提交
  public inputnull: boolean;
  //input 验证
  public inputZ: boolean;
  
  roles_focus(v) {
    this.subList = {
      id:v.id,
      name:v.name,
      description:v.description,
      createdate:v.createdate,
      operateType:"1"
    };
    this.inputnull = true;
  }
  roles_blur(){
    if(this.inputnull){
      var check = /^[a-zA-Z\d\u4E00-\u9FA5_]+$/;
      if (check.test(this.subList.name)) {
        this.inputnull = true;
        this.inputZ = true;
      } else {
        this.inputZ = false;
        this.getTabList(1, 20, this.searchData);
        this.server.prompt = { prompt: true, success: false, fail: true, promptStatement: "必填项不能修改为空,不能输入特殊字符,允许输入_" }
      }
      // if(this.subList.name == ''){
      //   this.inputnull = false;
      //   this.getTabList(1, 20, this.searchData);
      // }
      if(this.inputZ){
        let url = this.server.baseUrl + 'Service/Center/insertAndUpdateCenter';
        this.tableListService.postRequest(url, this.subList).then((res: any) => {
          console.log(res)
          if (res.result_code == '0000') {
            this.subList = { id: '', name: '', description: '', createdate: '',operateType:"1" };
            this.inputnull = false;
            this.server.prompt = { prompt: true, success: true, fail: false, promptStatement: res.message }
          }
        })
      }
      
    }else{
      console.log("没有输入哦")
    }
  }


  
  
}
